package com.zhijiaoyun.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zhijiaoyun.common.DsConstant;
import com.zhijiaoyun.common.StringUtils;
import com.zhijiaoyun.dao.ICollectionDao;
import com.zhijiaoyun.model.Collection;
import com.zhijiaoyun.model.dto.CollectionDto;
import com.zhijiaoyun.model.dto.CourseDto;
import com.zhijiaoyun.model.dto.NewsDto;
import com.zhijiaoyun.model.dto.PageData;
import com.zhijiaoyun.model.dto.PageList;
import com.zhijiaoyun.service.ICollectionService;
import com.zhijiaoyun.service.ICourseService;
import com.zhijiaoyun.service.INewsService;

/**
 * @author shisfish
 * @date 2017/6/6
 */
@Service(value = "collectionServie")
public class CollectionServiceImpl implements ICollectionService {

	@Resource
	ICollectionDao collectionDao;
	
	@Resource
	ICourseService courseService;
	
	@Resource
	INewsService newsService;
	
	@Override
	public Collection getByIds(Long relationId, Long userId, int collectionType) {
		return collectionDao.selectByIds(relationId, userId, collectionType);
	}

	@Override
	public void addCollection(Collection collection) {
		collectionDao.insert(collection);
	}

	@Override
	public Collection getByCollectionIdUserId(Long collectionId, Long userId) {
		return collectionDao.selectByCollectionIdUserId(collectionId, userId);
	}

	@Override
	public void deleteCollection(Collection collection) {
		collectionDao.deleteByPrimaryKey(collection.getCollectionId());
	}

	@Override
	public List<Collection> getByUserId(Integer pageIndex, Long userId, int type) {
		if (pageIndex == null || pageIndex <= 0) {
			pageIndex = 1;
		}
		int beginIndex = (pageIndex - 1) * DsConstant.PAGESIZE_DEFAULT;
		return collectionDao.selectByUserId(beginIndex, DsConstant.PAGESIZE_DEFAULT, userId, type);
	}
	
	@Override
	public List<CollectionDto> getDtoByUserId(Integer pageIndex, Long userId, int type) {
		if (pageIndex == null || pageIndex <= 0) {
			pageIndex = 1;
		}
		int beginIndex = (pageIndex - 1) * DsConstant.PAGESIZE_DEFAULT;
		List<CollectionDto> collectionDtos = new ArrayList<>();
		List<Collection> collections = collectionDao.selectByUserId(beginIndex, DsConstant.PAGESIZE_DEFAULT, userId, type);
		if (!StringUtils.isEmptyList(collections)) {
			for (Collection collection : collections) {
				// 收藏的对象
				setCourseProperty(collectionDtos, collection);
			}
		}
		return collectionDtos;
	}

	@Override
	public PageList<CollectionDto> pageDtoByUserId(Integer pageIndex, int pageSize, long userId, int type) {
		int count = collectionDao.countByParams(userId, type);
		PageData pageData = new PageData();
		List<CollectionDto> listD = new ArrayList<>();
		PageList<CollectionDto> pageList = new PageList<>();
		if (count > 0) {
			int pageIndexInt = pageIndex == null ? 1 : pageIndex;
			pageData = PageData.createPage(count, pageIndexInt, pageSize);
			List<Collection> collections = collectionDao.selectByUserId(pageData.getBeginIndex(), pageData.getPageSize(), userId, type);
			if (!StringUtils.isEmptyList(collections)) {
				for (Collection collection : collections) {
					setCourseProperty(listD, collection);

				}
			}
		}
		pageList = new PageList<>(listD, pageData);
		return pageList;
	}

	private void setCourseProperty(List<CollectionDto> listD, Collection collection) {
		// 收藏的对象
		if (collection.getCollectionType() == 1) {
            CourseDto courseDto = courseService.getCourseDtoByCourseId(collection.getRelationId());
            if (courseDto != null) {
                CollectionDto collectionDto = new CollectionDto(collection);
                collectionDto.setCourseDto(courseDto);
                listD.add(collectionDto);
            }
        } else {
            NewsDto newsDto = newsService.getNewsDtoByNewsId(collection.getRelationId());
            if (newsDto != null) {
                CollectionDto collectionDto = new CollectionDto(collection);
                collectionDto.setNewsDto(newsDto);
                listD.add(collectionDto);
            }
        }
	}

}
